<template>
  <div>
    <div class="login-container">
      <div class="login-title">面经登录</div>
      <!-- 用户名<input type="text" v-model="username" placeholder="用户名" />
      <br />

      密码<input type="password" v-model="password" placeholder="密码" />

      <button @click="login">提交</button>
      <br /> -->
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >提交</van-button
          >
        </div>
      </van-form>
      <router-link to="/register">注册账号</router-link>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    // onSubmit(values) {
    //   console.log("submit", values);
    // },

    async onSubmit() {
      if (!this.username || !this.password) {
        return;
      }
      let res = await axios({
        method: "post",
        url: "http://interview-api-t.itheima.net/h5/user/login",
        data: {
          username: this.username,
          password: this.password,
        },
      });
      // console.log(res);
      if (res.data.code == 10000) {
        localStorage.setItem("token", res.data.data.token);
        this.$router.push({ path: "/home/Article" });
      } else {
        alert(res.data.message);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-container button {
  width: 300px;
  background-color: #fa6d1d;
  color: white;
  height: 40px;
  border: none;
}
.login-container input {
  border: none;
}
</style>
